package com.china08.yxyapi.api.home.activity.lecture;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.huodong.lecture.YxyHuodongLectureResultTea;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.lectureresult.YxyHuodongLectureResultTeaRepository;

@CrossOrigin
@RestController
public class ActivityLectureYtTeaIndexController extends YxyApiBaseController {

    @Autowired
    private YxyHuodongLectureResultTeaRepository teaRepository;

    @RequestMapping(value = "/api_yw/home/activity/tea/lecture/yt/pack/index", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(//
            HttpServletRequest request, //
            HttpServletResponse response, // ,
            @RequestParam(value = "code", defaultValue = "610203") String code) {
        RespModel respModel = new RespModel();

        List<YxyHuodongLectureResultTea> list = teaRepository.findTop20ByAreaIdDistrictOrderByPackCountDesc(code);

        Integer countTea = teaRepository.countByAreaIdDistrict(code);
        if (countTea == null) {
            countTea = 0;
        }
        respModel.countTea = countTea;

        if (CollectionUtils.isNotEmpty(list)) {
            for (YxyHuodongLectureResultTea yxyHuodongLectureResultTea : list) {
                respModel.data.add(yxyHuodongLectureResultTea.getPackCount());
                respModel.name.add(yxyHuodongLectureResultTea.getTeaName());
            }
        }
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {
    }

    protected static class RespModel extends YxyApiRespModel {
        public List<String> name = new ArrayList<String>();
        public List<Long> data = new ArrayList<>();
        // 教师数
        public Integer countTea = 0;

    }
}
